import Vue from 'vue'
import App from './App.vue'
import router from './router'

import 'font-awesome/css/font-awesome.min.css'
import axios from 'axios'
import qs from 'qs'
import {
	getCurDate,
	setSessionStorage,
	getSessionStorage,
	removeSessionStorage,
	setLocalStorage,
	getLocalStorage,
	removeLocalStorage
} from './common.js'

Vue.config.productionTip = false

//设置axios的基础url部分
axios.defaults.baseURL = 'http://172.23.150.172:8080/api/';
//将axios挂载到vue实例上，使用时就可以 this.$axios 这样使用了
Vue.prototype.$axios = axios;

Vue.prototype.$qs = qs;

Vue.prototype.$getCurDate = getCurDate;
Vue.prototype.$setSessionStorage = setSessionStorage;
Vue.prototype.$getSessionStorage = getSessionStorage;
Vue.prototype.$removeSessionStorage = removeSessionStorage;
Vue.prototype.$setLocalStorage = setLocalStorage;
Vue.prototype.$getLocalStorage = getLocalStorage;
Vue.prototype.$removeLocalStorage = removeLocalStorage;

router.beforeEach(function (to, from, next) {
	let token = localStorage.getItem('id_token');
	//除了登录、注册、首页、商家列表、商家信息之外，都需要判断是否登录
	const publicRoutes = ['/', '/index', '/businessList', '/businessInfo', '/login', '/register', '/passwordChange', '/personalCenter', '/addFood', '/payment'];
	const walletRoutes = ['/wallet', '/walletRecharge', '/walletWithdraw', '/walletTransactions', '/walletPayment', '/walletFreezeDetail'];

	if (!publicRoutes.includes(to.path) && !walletRoutes.includes(to.path)) {
		if (token == null) {
			router.push('/login');
			location.reload();
			return;
		}
	}

	// 钱包相关页面单独验证登录状态
	if (walletRoutes.includes(to.path)) {
		if (token == null) {
			router.push('/login');
			location.reload();
			return;
		}
	}

	next();
});

new Vue({
	router,
	render: h => h(App)
}).$mount('#app')